package com.ruoyi.web.service;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Component
public class SysModFactory {

    @Qualifier("SysModDataService")
    @Autowired
    private SysModDataService sysModDataService;


    /**
     * key：模型ID
     * value：模型重写业务实现类
     */
    private static final Map<String, SysModDataService> SERVICE_MAP = new HashMap<>();

    @PostConstruct
    private void init() {
        SERVICE_MAP.put("-1", sysModDataService);
    }

    public static void register(String modId, SysModDataService service) {
        SERVICE_MAP.put(modId, service);
    }

    public static SysModDataService getSysModDataService(String modId) {

        if (StringUtils.isEmpty(modId)) {
            return SERVICE_MAP.get("-1");
        }

        SysModDataService service = SERVICE_MAP.get(modId);

        return service == null ? SERVICE_MAP.get("-1") : service;
    }
}
